//
//  UISliderView.m
//  firstDemo
//
//  Created by junhong.zhu@lachesis-mh.com on 16/5/18.
//  Copyright © 2016年 LACHESIS. All rights reserved.
//

#import "UISliderView.h"

#define kSliderRadius 15

@interface UIPopView : UIView

@property (nonatomic, assign) NSUInteger day;

@end

@interface UISliderView ()


@property (nonatomic, strong) UIPopView *popView;

@property (nonatomic, strong) UISlider  *slider;

@end

@implementation UISliderView


#pragma mark -
#pragma mark life cycle
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self addSubview:self.popView];
    [self addSubview:self.slider];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self movePopView:24];
    });
    
}

#pragma mark -
#pragma mark priveti

- (void)movePopView:(CGFloat)sliderValue
{
    NSInteger integer = sliderValue;
    
    self.popView.day = integer;
    
    [self.slider setValue:integer animated:YES];
    
    CGPoint popViewCenter = self.popView.center;
    CGFloat sliderWidth = self.slider.frame.size.width;
    
    popViewCenter.x = self.slider.value / self.slider.maximumValue * sliderWidth;
    
    // 比例计算
    popViewCenter.x = popViewCenter.x / sliderWidth * (sliderWidth - kSliderRadius * 2) + kSliderRadius + self.slider.frame.origin.x;
    self.popView.center = popViewCenter;
}

#pragma mark -
#pragma mark evenhandle
- (void)sliderValueChange:(UISlider *)slider
{
    [self movePopView:slider.value];
}

#pragma mark -
#pragma mark overwrite
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIBezierPath *topLinePath = [UIBezierPath bezierPath];
    topLinePath.lineWidth = 1;
    [topLinePath moveToPoint:CGPointMake(0, 1)];
    [topLinePath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), 1)];
    [topLinePath stroke];
    
    NSDictionary *attribute = @{NSFontAttributeName : [UIFont systemFontOfSize:17],
                                NSForegroundColorAttributeName : [UIColor blackColor]};
    
    CGSize textSize = [@"入院" boundingRectWithSize:CGSizeMake(60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    //CGFloat x = (60 - textSize.width) * 0.5;
    //CGFloat y = self.slider.center.y - textSize.height * 0.5;
    CGFloat x = 30;
    CGFloat y = self.slider.center.y - 15;
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_shouye_arrow"]];
    
//    [@"入院" drawInRect:({
//        CGRectMake(x, y, 60, self.slider.frame.size.height);
//    }) withAttributes:attribute];
//    
//    [@"出院" drawInRect:({
//        CGRectMake(x + CGRectGetMaxX(self.slider.frame), y, 60, self.slider.frame.size.height);
//    }) withAttributes:attribute];
    [image drawRect:CGRectMake(x, y, 60, self.slider.frame.size.height)];
//    [image drawInRect:({
//        CGRectMake(x, y, 60, self.slider.frame.size.height);
//    }) withAttributes:attribute];
//
    
   // [image drawRect: CGRectMake(x + CGRectGetMaxX(self.slider.frame), y, 60, self.slider.frame.size.height)];

//    [image drawInRect:({
//        CGRectMake(x + CGRectGetMaxX(self.slider.frame), y, 60, self.slider.frame.size.height);
//    }) withAttributes:attribute];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.slider setFrame:({
        CGRectMake(60, 44, self.frame.size.width - 120, self.slider.frame.size.height);
    })];
}


#pragma mark -
#pragma mark lazy load

- (UISlider *)slider
{
    if (!_slider) {
        _slider = [[UISlider alloc] init];
        _slider.minimumValue = 1;
        _slider.maximumValue = 24;
        _slider.value = 5;
        [_slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _slider;
}
- (UIPopView *)popView
{
    if (!_popView) {
        _popView = [[UIPopView alloc] initWithFrame:CGRectMake(0, 14, 44, 30)];
        _popView.backgroundColor = [UIColor whiteColor];
    }
    return _popView;
}

@end


/**
 *  >>>>>>>>>>>popView
 */


@interface UIPopView ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation UIPopView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.label];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.label sizeToFit];
    [self.label setCenter:({
        CGFloat x = self.frame.size.width * 0.5;
        CGFloat y = self.frame.size.height * 0.4;
        CGPointMake(x, y);
    })];
}

- (void)setDay:(NSUInteger)day
{
    _day = day;
    self.label.text = [NSString stringWithFormat:@"%zd月", day];
    [self setNeedsLayout];
}

- (void)drawRect:(CGRect)rect
{
    
    /** 
     
        a-------------b
        |             |
        |             |
        g----f   d----c
              \ /
               e
        a minX      , minY
        b maxX      , minY
        c maxX      , midY
        d midX + 4  , midY
        e midX      , maxY
        f midX - 4  , midY
        g minX      , midY
     */
    
    CGFloat maxX = CGRectGetMaxX(rect) - 1;
    CGFloat maxY = CGRectGetMaxY(rect) - 1;
    
    CGFloat minX = CGRectGetMinX(rect) + 1;
    CGFloat minY = CGRectGetMinY(rect) + 1;
    
    CGFloat midX = CGRectGetMidX(rect);
    CGFloat midY = 22;
    
    UIBezierPath *linePath = [[UIBezierPath alloc] init];
    linePath.lineWidth = 1;
    [[UIColor colorWithRed:43 / 255.0 green:90 / 255.0 blue:142 / 255.0 alpha:1] set];
    [linePath moveToPoint:CGPointMake(minX, minY)];
    [linePath addLineToPoint:CGPointMake(maxX    , minY)];
    [linePath addLineToPoint:CGPointMake(maxX    , midY)];
    [linePath addLineToPoint:CGPointMake(midX + 4, midY)];
    [linePath addLineToPoint:CGPointMake(midX    , maxY)];
    [linePath addLineToPoint:CGPointMake(midX - 4, midY)];
    [linePath addLineToPoint:CGPointMake(minX    , midY)];
    [linePath addLineToPoint:CGPointMake(minX    , minY)];
    
    [linePath stroke];
    
}

#pragma mark -
#pragma mark lazyload

- (UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:13];
        _label.textColor = [UIColor colorWithRed:43 / 255.0 green:90 / 255.0 blue:142 / 255.0 alpha:1];
        
    }
    return _label;
}

@end




